    package web;  
     
    import database.entity.Product;
    import database.entity.User;
    import database.manager.ProductManager;
    import database.manager.UserManager;
    import java.util.ArrayList;  
    import java.util.List;  
    import javax.faces.model.DataModel;  
    import javax.faces.model.ListDataModel;  
    import javax.faces.model.SelectItem;  

     
    public class WebManagedBean {  
     
       // objetos del modelo para usar en la vista  
       private Product product = new Product();  
       private User user = new User();
       
         
       // gestion del modelo  
       private ProductManager productManager = new ProductManager(); // acceso a los datos de product  
       private UserManager userManager = new UserManager(); // acceso a los datos de user
      
       
	   private DataModel data = null;  
       
     
       public WebManagedBean() { 
    	   
       }  

     
       public DataModel getData() {  
           if(data == null){  
               refreshListProduct();  
           }  
           return data;  
       }  
     
       public void setData(DataModel data) {  
           this.data = data;  
       }  
     
       public String newProduct(){  
           if(!productManager.checkRepeated("name", product.getName())){  
               productManager.create(product);  
           }  
           return gotoAdmin();  
       }  
     
       public String updateProduct(){  
           productManager.update(product);  
           return gotoAdmin2();  
       }  
     
       public String deleteProduct(){  
           product = (Product) data.getRowData();  
           productManager.delete(product);  
           return gotoAdmin2();  
       }  
     
       public String gotoIndex(){  
           return "index";  
       }  
       
       public String gotoAdmin(){  
           refreshListProduct();  
           return "redirectAdmin";  
       }  
       
       public String gotoAdmin2(){  
           refreshListProduct();  
           return "admin";  
       }  
     
       public String gotoUpdate(){  
           product = (Product) data.getRowData();  
           return "update";  
       } 
       
       public String gotoRegister(){  
    	   return "register";  
       } 
     
       private void refreshListProduct(){  
           List<Product> list = productManager.getList();  
           data = new ListDataModel(list); // lista actualizada  
       }  
     
       
	
	   	public Product getProduct() {
		return product;
	   	}

		public void setProduct(Product product) {
			this.product = product;
		}
	
		public UserManager getUserManager() {
			return userManager;
		}
	
		public void setUserManager(UserManager userManager) {
			this.userManager = userManager;
		}
	
		public ProductManager getProductManager() {
			return productManager;
		}
	
		public void setProductManager(ProductManager productManager) {
			this.productManager = productManager;
		}

		public User getUser() {
			return user;
	   	}

		public void setUser(User user) {
			this.user = user;
		}

		
		public String logged() {
			Boolean containsUser=false;
			 List<User> listUsers = userManager.getList();
			 for(User u : listUsers){
				 if(u.getUserName().equals(user.getUserName()) && u.getPassword().equals(user.getPassword())){
					 containsUser=true;
				 }
			 }
			
	        if (containsUser && user.getUserName().equals("admin")){
	        	return "redirectAdmin";
	        }else if(containsUser){
	        	return "redirectUser";
	        }else{
	        	return "failure";
	        }
	    }
		
		
		public String newUser(){ 
			 if(!userManager.checkRepeated("userName", user.getUserName())){  
	               userManager.create(user);  
	         }  
	           return gotoIndex();  
	    }  
       
       
    } 